<?php
namespace app\models;

//装箱产品排柜表

use yii;
use yii\db\Query;

class ContainerProductModel extends BaseModel
{
    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'container_product';
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) {
            $db->andWhere(['id' => $where['id']]);
        }

        return $db->one();
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['flag'])) {
            $db->andWhere(['flag' => $where['flag']]);
        }

        if (!empty($where['id'])) {
            if (is_array($where['id'])) {
                $db->andWhere(['in', 'id', $where['id']]);
            } else {
                $db->andWhere(['id' => $where['id']]);
            }
        }
        if (isset($where['split_pid'])) {
            if (is_array($where['split_pid'])) {
                $db->andWhere(['in', 'split_pid', $where['split_pid']]);
            } else {
                $db->andWhere(['split_pid' => $where['split_pid']]);
            }
        }
        if ((isset($where['purchase_num'])) && (!empty($where['purchase_num']))) {
            if (is_array($where['purchase_num'])) {
                $db->andWhere(['in', 'purchase_num', $where['purchase_num']]);
            } else {
                $db->andWhere(['purchase_num' => $where['purchase_num']]);
            }
        }
        if (isset($where['container_num_empty'])) {
            if ($where['container_num_empty']) {
                $db->andWhere(['container_num' => '']);
            } else {
                $db->andWhere(['!=', 'container_num', '']);
            }
        }
        if (isset($where['container_num'])) {
            $db->andWhere(['container_num' => $where['container_num']]);
        }
        if (isset($where['status'])) {
            if (is_array($where['status'])) {
                $db->andWhere(['in', 'status', $where['status']]);
            } else {
                $db->andWhere(['status' => $where['status']]);
            }
        }
        if (!empty($where['begin_time'])) {
            $db->andWhere(['>=', 'create_at', strtotime($where['begin_time'])]);
        }
        if (!empty($where['end_time'])) {
            $db->andWhere(['<=', 'create_at', strtotime($where['end_time'])]);
        }
        if (!empty($where['name'])) {
            $db->andWhere(['like', 'name', $where['name']]);
        }
        if ($offset) {
            $db->offset($offset);
        }

        if ($limit) {
            $db->limit($limit);
        }

        if ($order) {
            $db->orderBy($order);
        }

        return $db->all();

    }

    public function size($where = [])
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if (!empty($where['flag'])) {
            $db->andWhere(['flag' => $where['flag']]);
        }
        if (!empty($where['id'])) {
            if (is_array($where['id'])) {
                $db->andWhere(['in', 'id', $where['id']]);
            } else {
                $db->andWhere(['id' => $where['id']]);
            }
        }
        if (isset($where['split_pid'])) {
            if (is_array($where['split_pid'])) {
                $db->andWhere(['in', 'split_pid', $where['split_pid']]);
            } else {
                $db->andWhere(['split_pid' => $where['split_pid']]);
            }
        }
        if ((isset($where['purchase_num'])) && (!empty($where['purchase_num']))) {
            if (is_array($where['purchase_num'])) {
                $db->andWhere(['in', 'purchase_num', $where['purchase_num']]);
            } else {
                $db->andWhere(['purchase_num' => $where['purchase_num']]);
            }
        }
        if (isset($where['container_num_empty'])) {
            if ($where['container_num_empty']) {
                $db->andWhere(['container_num' => '']);
            } else {
                $db->andWhere(['!=', 'container_num', '']);
            }
        }
        if (isset($where['container_num'])) {
            $db->andWhere(['container_num' => $where['container_num']]);
        }
        if (isset($where['status'])) {
            if (is_array($where['status'])) {
                $db->andWhere(['in', 'status', $where['status']]);
            } else {
                $db->andWhere(['status' => $where['status']]);
            }
        }
        if (!empty($where['begin_time'])) {
            $db->andWhere(['>=', 'create_at', strtotime($where['begin_time'])]);
        }
        if (!empty($where['end_time'])) {
            $db->andWhere(['<=', 'create_at', strtotime($where['end_time'])]);
        }
        if (!empty($where['name'])) {
            $db->andWhere(['like', 'name', $where['name']]);
        }
        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    /**
     * 通过装箱编号获取采购编号
     *
     */
    public function getPurchaseByNumber($number)
    {
        $db = new Query();
        return $db->select('distinct(purchase_num) as num')->from($this->table)
            ->where(['container_num' => $number])
            ->all();
    }
}
